package DAO;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class DialogCalendar {

    private Date date;
    private JButton[] but;
    private Calendar cal;
    private String day = "";
    private int month = cal.getInstance().get(Calendar.MONTH);
    private int year = cal.getInstance().get(Calendar.YEAR);
    private JLabel labelInCal = new JLabel("", JLabel.CENTER);

    public void MakeCalendar() {
        final JDialog dialog = new JDialog();

        JPanel panelControl = new JPanel(new GridLayout(1, 3));
        JButton butNext = new JButton("Next Month");
        JButton butPrev = new JButton("Previous Month");

        butNext.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                month++;
                displayDate();
                setVisibleDate();
            }
        }); // End butNext Action

        butPrev.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                month--;
                displayDate();
                setVisibleDate();
            }
        }); // End butPrev Action
        panelControl.add(butPrev);
        panelControl.add(labelInCal);
        panelControl.add(butNext);

        JPanel panelCal = new JPanel(new GridLayout(7, 7));
        panelCal.setPreferredSize(new Dimension(370, 150));

        but = new JButton[49];
        String[] dayName = {"Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"};
        for (int i = 0; i < but.length; i++) {
            final int select = i;
            but[i] = new JButton();
            if (i < 7) {
                but[i].setText(dayName[i]);
                but[i].setForeground(Color.red);
            }

            if (i > 6) {
                but[i].setText("");
                but[i].setBackground(Color.WHITE);
                but[i].addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        day = but[select].getActionCommand();
                        dialog.dispose();
                    }
                });
            }
            panelCal.add(but[i]);
        }   // End for


        dialog.add(panelControl, BorderLayout.NORTH);
        dialog.add(panelCal, BorderLayout.CENTER);
        dialog.pack();
        dialog.setModal(true);
        dialog.setLocationRelativeTo(null);
        displayDate();
        setVisibleDate();
        dialog.setVisible(true);
    } // End MakeCalendar()

    private void displayDate() {
        for (int i = 7; i < but.length; i++) {
            but[i].setText("");
        }

        cal = Calendar.getInstance();
        cal.set(year, month, 1);

        int DoW = cal.get(Calendar.DAY_OF_WEEK);
        int DoM = cal.getActualMaximum(Calendar.DAY_OF_MONTH);

        for (int i = 6 + DoW, day = 1; day <= DoM; i++, day++) {
            but[i].setText("" + day);
        }

        DateFormat formatDate = new SimpleDateFormat("yyyy - MMMM");
        labelInCal.setText(formatDate.format(cal.getTime()));


    } // End displayDate()

    public String setSelectedDate() {
        if (day.equals("")) {
            return "";
        }

        cal.set(year, month, Integer.parseInt(day));
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
        date = cal.getTime();
        return dateFormat.format(cal.getTime());
    }

    public void setVisibleDate() {
        for (int i = 7; i < but.length; i++) {
            but[i].setEnabled(true);
            if (but[i].getText().equals("")) {
                but[i].setEnabled(false);
            }
        }
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }
} // End DialogCalendar
